home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swaga_c.zip / COLOR.SWG / 0002_Colors Bits.pas < prev    next >
Pascal/Delphi Source File  |  1993-05-28  |  3KB  |  85 lines

  1. {
  2. MICHAEL NICOLAI
  3.  
  4. > I need to extract the foreground color (black) and the background color
  5. > (cyan) and insert them into vars for another procedure, that calls a
  6. > picklist with Fg,Bg attributes. I can't change the way the procedure/
  7. > function works, so I need to feed it =my= colors in =its= format.
  8. >
  9.  
  10. Do you know the format of the attribute-byte? If not, here it is:
  11.  
  12.  Bit  7 6 5 4 3 2 1 0
  13.       B b b b I f f f
  14.  
  15. B   - 0 do not blink
  16.       1 character is blinking
  17.  
  18. bbb - backgroundcolor (3 Bit, giving you a total of 8 different values.)
  19.  
  20. I   - 0 foregroundcolor is not intensified
  21.       1 foregroundcolor is intensified
  22.  
  23. fff - foregroundcolor (3 Bit + I, giving you a total of 16 different values.)
  24.  
  25.  
  26. If you now want to extract the fore- or backgroundcolor you can easily do
  27. that by performing an AND with either 70h, 0Fh or 07h.
  28.  
  29. The operator AND (if you don't know it):
  30.  
  31.    AND  a b | x      a & b = x   (or in Pascal: x := a and b;
  32.        ---------
  33.         0 0 | 0
  34.         0 1 | 0
  35.         1 0 | 0
  36.         1 1 | 1
  37.  
  38. As you see, only when b is set to 1, the value in a is "getting through".
  39.  
  40. For example: a = 1011000111010111, b = 0001011011110110
  41. then
  42.  
  43.                    1011000111010111
  44.                  & 0001011011110110
  45.                 --------------------
  46.                    0001000011010110
  47.  
  48. When you look at it for a while you will see that, only where there is a 1
  49. in the lower number, the value in the upper number is represented in the
  50. result. Hence, you can use the AND operator to mask a portion of a number.
  51.  
  52. Now, let's get back to your colors: You mentioned 48 or NORM.
  53. 48 decimal equals to 00110000b. That is 'Not Blink', 'Color 3 for
  54. Background', 'Color 0 for Foreground' and 'Foregroundcolor not intensified'.
  55.  
  56. What do you get, if you perform NORM & 70h? Let's see:
  57.  
  58.           NORM   00110000
  59.         &  70h   01110000
  60.       ---------------------
  61.                  00110000      (= Backgroundcolor or Bg)
  62.  
  63. Not much you think, hm? Ok, but that has to do with the initial number NORM.
  64. You will see "the light" as we proceed. :-)
  65.  
  66. Now, let us perform NORM & 0Fh:
  67.  
  68.           NORM   00110000
  69.         &  0Fh   00001111
  70.       ---------------------
  71.                  00000000      (= Foregroundcolor WITH I)
  72.  
  73. and NORM & 07h:
  74.  
  75.           NORM   00110000
  76.         &  07h   00000111
  77.       ---------------------
  78.                  00000000      (= Foregroundcolor WITHOUT I)
  79.  
  80.  
  81. Hm, somewhat NORM was a bad choice as an example. But if you try it with
  82. other values you will see how easy it is to "get a few bits out of a byte"!
  83. }
  84.  
  85.